<?php

// +----------------------------------------------------------------------
// | LuluCWS [ Lulu COMPANY WEB SHOW]
// +----------------------------------------------------------------------
// | Copyright (c) 2010 http://www.luluui.com All rights reserved.
// +----------------------------------------------------------------------
// | Licensed ( http://www.apache.org/licenses/LICENSE-2.0 )
// +----------------------------------------------------------------------
// | Author: uuleaf <uuleaf@163.com>
// +----------------------------------------------------------------------
class DivideModel extends IbaseModel {
	/**
	 +----------------------------------------------------------
	 * 字段设置数组
	 +----------------------------------------------------------
	 * @access public
	 +----------------------------------------------------------
	 * @param none
	 +----------------------------------------------------------
	 * @return none
	 * '字段名称' => array();
	 * t 字体类型 ：input text function select relation
	 * v 默认值
	 +----------------------------------------------------------
	 * @throws ThinkExecption
	 +----------------------------------------------------------
	 */
	public $fields_set = array (
		'divide_id' => array (
			't' => 'input',
			'v' => 0
		),
		'member_id' => array (
			't' => 'input',
			'v' => 0
		),
		'member_name' => array (
			't' => 'input',
			'v' => 0
		),
		'orderform_id' => array (
			't' => 'input',
			'v' => 0
		),
		'orderform_code' => array (
			't' => 'input',
			'v' => 0
		),
		'orderform_fee' => array (
			't' => 'input',
			'v' => 0
		),
		'orderform_happen' => array (
			't' => 'input',
			'v' => 0
		),
		'orderform_over' => array (
			't' => 'input',
			'v' => 0
		),
		'rate' => array (
			't' => 'input',
			'v' => 0
		),
		'fee' => array (
			't' => 'input',
			'v' => 0
		),
		'settled' => array (
			't' => 'input',
			'v' => 0
		),
		'is_settled' => array (
			't' => 'input',
			'v' => 0
		),
		'settle_id' => array (
			't' => 'select',
			'v' => 0
		),
		'note' => array (
			't' => 'input',
			'v' => ''
		)
	);

	public function doNew($orderinfo, $rate) {
		$divide_id = cookie('divide_id');
		//dump($orderinfo);
		if (empty ($rate))
			return false;
		if (empty ($divide_id))
			return false;
		if (empty ($orderinfo))
			return false;
		//非注册用户订单
		if (empty ($orderinfo['member_id']))
			return false;
		//自己的订单
		if ($divide_id == $orderinfo['member_id'])
			return false;
		$datainfo = array (
			'divide_id' => '',
			'member_id' => 0,
			'member_name' => '',
			'dmember_id' => $divide_id,
			'dmember_name' => '',
			'orderform_id' => '',
			'orderform_code' => '',
			'orderform_fee' => '',
			'orderform_happened' => '',
			'orderform_overed' => '',
			'orderform_ok' => 0,
			'rate' => $rate,
			'fee' => 0,
			'settled' => 0,
			'is_settled' => 0,
			'settle_id' => 0,
			'note' => '',
			
		);
		//下单会员Info
		$mmem = M('Member');
		if (!empty ($orderinfo['member_id'])) {
			$memmap['member_id'] = $orderinfo['member_id'];
			$meminfo = $mmem->where($memmap)->find();
			if ($meminfo) {
				$datainfo['member_id'] = $meminfo['member_id'];
				$datainfo['member_name'] = $meminfo['username'];
			}
		}

		//佣金会员Info
		$memmap['member_id'] = $divide_id;
		$meminfo = $mmem->where($memmap)->find();
		if ($meminfo) {
			$datainfo['dmember_id'] = $meminfo['member_id'];
			$datainfo['dmember_name'] = $meminfo['username'];
		} else {
			return false;
		}
		//订单Info
		$morder = M('Orderform');
		$ormap['code'] = $orderinfo['code'];
		$oinfo = $morder->where($ormap)->find();
		if ($oinfo) {
			$datainfo['orderform_id'] = $oinfo['orderform_id'];
			$datainfo['orderform_code'] = $oinfo['code'];
			$datainfo['orderform_fee'] = $oinfo['total_fee'];
			$datainfo['orderform_happened'] = $oinfo['created'];
			$datainfo['orderform_overed'] = 0;
			$datainfo['orderform_ok'] = 0;

		} else {
			return false;
		}

		//提成
		$datainfo['fee'] = round((((int) $datainfo['orderform_fee']) * $rate) / 100, 2);
		$rs = $this->add($datainfo);
		if ($datainfo) {
			$odata = array (
				'divide_id' => $divide_id,
				'is_divide' => 1
			);
			$morder->where($ormap)->save($odata);
		} else {
			return false;
		}
		return true;

	}
}
?>